from flask import Flask, Blueprint, render_template, request, redirect, session, flash, url_for, current_app
from ..utils.config import DEFAULT_PARAMS

sg = Blueprint(name="setting", import_name=__name__, url_prefix='/')


@sg.route("/setting", methods=['GET', 'POST'])
def settings():
    if request.method == "GET":
        if 'population_size' not in session:
            session['population_size'] = current_app.config['DEFAULT_POPULATION_SIZE']
        if 'n_generations' not in session:
            session['n_generations'] = current_app.config['DEFAULT_N_GENERATIONS']
        if 'max_capacity' not in session:
            session['max_capacity'] = current_app.config['DEFAULT_MAX_CAPACITY']
        if 'mutation_rate' not in session:
            session['mutation_rate'] = current_app.config['DEFAULT_MUTATION_RATE']
        print(session.get('population_size'))
        print(session.get('n_generations'))
        print(session.get('max_capacity'))
        print(session.get('mutation_rate'))
        return render_template('setting.html')

    if request.method == "POST":
        session['population_size'] = request.form.get('population_size', type=int)
        session['n_generations'] = request.form.get('n_generations', type=int)
        session['max_capacity'] = request.form.get('max_capacity', type=int)
        session['mutation_rate'] = request.form.get('mutation_rate', type=float)

        flash('参数修改成功！', 'success')
        return redirect('/setting')
